//
//  MVDetailsViewController.m
//  SunXi
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "MVDetailsViewController.h"

@interface MVDetailsViewController ()<UIWebViewDelegate>
@property (nonatomic, strong)CoreDataManager *manager;

@end

@implementation MVDetailsViewController{
    UIButton *_Collerbutton;
    BOOL _bollcollect;
    NSArray *_collarray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [CoreDataManager defaults];

    self.title = self.mytitle;
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    
    [SV addSubview:webView];
    webView.delegate = self;
    webView.scalesPageToFit = YES;
    
    NSURL *url = [NSURL URLWithString:self.str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.delegate = self;
    
    [ShowMytext showHUDloadView:SV];

    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(hidd) userInfo:nil repeats:NO];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    [self.navigationItem.rightBarButtonItem setTintColor:BUTTONCOLOR];
    _Collerbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    _Collerbutton.frame = CGRectMake(WIDTH - 70, 10, 24, 24);
    [self.navigationController.navigationBar.viewForBaselineLayout addSubview:_Collerbutton];
    self.manager = [CoreDataManager defaults];
    
    
    _bollcollect = [self lookcollect];
    
    if (_bollcollect) {
        [_Collerbutton setImage:[UIImage imageNamed:@"ysc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:[UIColor redColor]];
    }else{
        [_Collerbutton setImage:[UIImage imageNamed:@"wsc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:BUTTONCOLOR];
    }
    
    
    [_Collerbutton addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    /**
     *  查询是否收藏过
     */
}
- (void)collect:(UIBarButtonItem *)buttonItem{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collect" inManagedObjectContext:self.manager.managedObjectContext];
    
    if (_bollcollect) {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:entity];
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"title",@"%@"],self.mytitle];
        [fetchRequest setPredicate:predicate];
        NSError *error = nil;
        NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        for (Collect *temp in fetchedObjects) {
            [self.manager.managedObjectContext deleteObject:temp];
        }
        [self.manager saveContext];
        [_Collerbutton setImage:[UIImage imageNamed:@"wsc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:BUTTONCOLOR];
        _bollcollect = NO;
    }else{
        Collect *ect = [[Collect alloc]initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
        ect.type = @"movie";
        ect.code = self.str;
        ect.title = self.mytitle;
        ect.number =@0;
        [self.manager saveContext];
        [_Collerbutton setImage:[UIImage imageNamed:@"ysc"] forState:UIControlStateNormal];
        [_Collerbutton setTintColor:[UIColor redColor]];
        _bollcollect = YES;
    }
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    [super viewDidDisappear:animated];
    [_Collerbutton removeFromSuperview];
}
- (BOOL)lookcollect{
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Collect"];
    
    //添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",@"title",@"%@"],self.mytitle];
    request.predicate = predicate;
    
    NSError *error;
    _collarray = [[self.manager managedObjectContext] executeFetchRequest:request error:&error];
    
    if (_collarray.count > 0) {
        return YES;
    }
    
    return NO;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [ShowMytext hiddHUDloadView];
}
- (void)hidd{
    [ShowMytext hiddHUDloadView];

}
- (void)share{
    [shareModel sharedInstance].title = self.mytitle;
    [shareModel sharedInstance].type = Movie;
    [shareModel sharedInstance].code = self.str;
    [[ShareWindow sharedInstance] show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
